var MP3Comment = {
	urlGet:MP3.INTERACTION_URL+'comment',
	urlRequest:MP3.MP3_URL+'comment',
	container:'_commentList',
	type:'song',
	loading:false,
	defaultValue:'Hãy chia sẻ cảm nghĩ của bạn về bài hát này.',
	chars:0,
	init:function(inp,btnSubmit)
	{
		inp = '_txtComment';
		btnSubmit = '_btnCommentSubmit';

		if(zm('#_comment').size()){
			MP3Comment.type = zm('#'+MP3Comment.container).attr('otype');
			zm('#'+btnSubmit).click(function(){
				return MP3Comment.addComment('_mbxComment',zm('#'+inp));
			});
			zm('#_commnetNotiBtnC').click(function(){
				zm('#_commentNotiBox').addClass(Common.cssHidden);
			});
			zm('#'+inp).click(function(){
				if(this.value==MP3Comment.defaultValue) this.value='';
				MP3Comment.charsCounter(inp);
			});
			zm('#'+inp).keydown(function(e){
				if(e.keyCode==13 && !e.shiftKey) {
					MP3Comment.addComment('_mbxComment',zm('#'+inp));
					return false;
				} else if(e.keyCode == 32){
					var content = zm('#'+inp).val();
					return content.length>0?content.charCodeAt(content.length-1)!=32:false;
				}
			}).keyup(function(e){
					MP3Comment.charsCounter(inp);
			}).focus(function(e){
					MP3Comment.charsCounter(inp);
			});
			var idItem =zm('#'+MP3Comment.container).attr('oid');
			MP3Comment.refresh(idItem, 1,true);
		}
	},
	charsCounter:function(inp){
		var content = zm('#'+inp).val();
		var len = content.length;
		if(len>0 && content.charCodeAt(len-1)==32) len--;
		zm('#_commentChars').html(len +' ký tự');
	},
	renderPage:function()
	{
		var idItem =zm('#'+MP3Comment.container).attr('oid');
		zm('._commentDel').click(function(){
			MP3Comment.removeComment(idItem,zm(this).attr('rel'));
		});
		zm('.pageLink').click(function(){
			MP3Comment.refresh(idItem, zm(this).attr('rel'));
		});
		zm('._contentComment').each(function(){
			zm(this).html(MP3BadWord.filter(zm(this).html()));
		});
		zm('.comment-time').each(function(){
			var val = zm(this).html();
			var regEx = /\d+/;
			var match = regEx.exec(val);
			if(match){
				val = val.replace(match[0], zmCore.formatDateTime(match[0]));
				zm(this).html(val);
			}
		});
	},
	showMsg:function(errDiv, isErr,msg){
		zm('#_commentNotiBox').addClass(Common.cssHidden);
		if(isErr){			
			zm('#_msgCommentDiv').addClass(Common.cssHidden);
			zm('#_errCommentDiv').removeClass(Common.cssHidden).html(Common.getBoxErr(msg,'_errCommentBtnC'));
			zm('#_errCommentBtnC').click( function () {
				zm('#_errCommentDiv').addClass(Common.cssHidden)
				zm('#_commentNotiBox').removeClass(Common.cssHidden);
			});
		} else {
			zm('#_errCommentDiv').addClass(Common.cssHidden);
			zm('#_msgCommentDiv').removeClass(Common.cssHidden).html(Common.getBoxMsg(msg,'_msgCommentBtnC'));
			zm('#_msgCommentBtnC').click( function () {
				zm('#_msgCommentDiv').addClass(Common.cssHidden)
				zm('#_commentNotiBox').removeClass(Common.cssHidden);
			});
		}
	},
	addComment:function(errDiv, obj){		
		if(Login.show())
		{
			var idItem = obj.attr('oid');
			var content = obj.val();
			content = content.trim();
			zm('#_txtComment').val(content);
			if(content == MP3Comment.defaultValue)
			{
				MP3Comment.showMsg(errDiv, 1, 'Bạn cần nhập nội dung bình luận.');
			}
			else if(content.length<50)
			{
				MP3Comment.showMsg(errDiv, 1, 'Nội dung bình luận cần ít nhất 50 ký tự.');
			}
			else if(content.length>900)
			{
				MP3Comment.showMsg(errDiv, 1, 'Nội dung bình luận chỉ cho phép tối đa 900 ký tự.');
			}
			else if (idItem && !MP3Comment.loading) {
				_gaq.push(['mp3._trackPageview', '/comment/'+MP3Comment.type+'/'+location.pathname]);
				MP3Comment.loading = true;
				zmCore.post(MP3Comment.urlRequest+'/add-v2', 
					{'oid': idItem, 'content': content,'type':MP3Comment.type},
					{'dataType': 'json'},
					function(data) {
						MP3Comment.loading = false;
						if(!data.is_error)
						{
							if(data.result){
								zm('#'+MP3Comment.container).html(data.result);
								zm('#'+MP3Comment.container).attr('cpage',1);
								zm('#_pagination').html(data.paging);
								zm('#_commentCounter').html('('+data.total+')');
								MP3Comment.renderPage();
								//zm('.txtComment').renderEmotions();
								if(typeof ZME_widget_mix =='function') ZME_widget_mix();
								else zmCore.addScript(MP3.ME_WIDGET_URL + 'js/zingme_widget_ex_1.7.1.min.js', function () {ZME_widget_mix();});
							}
							zm('#_txtComment').val('');
						}
						MP3Comment.showMsg(errDiv, data.is_error, data.note);
						MP3Comment.charsCounter(inp);
				});
			}
		}
	},
	removeComment:function(oid,cid){
		zmCore.post(MP3Comment.urlRequest+'/remove',
			{'oid': oid, 'cid': cid,'type':MP3Comment.type},
			{'dataType': 'json'},
			function(data) {
				if(!data.is_error)
				{
					var cpage = zm('#'+MP3Comment.container).attr('cpage');
					MP3Comment.refresh(oid, cpage, true);
				}
		});
	},
	refresh:function(idItem,page, force)
	{
		page = parseInt(page);
		if(force=='undefined' || !force)
		{
			var cpage = zm('#'+MP3Comment.container).attr('cpage');
			force = cpage=='undefined'?false:!(page==parseInt(cpage));
		}
		if(force)
		{
			var url = MP3Comment.urlGet+'/loadpage-v2?oid='+idItem+'&page='+page+'&type='+MP3Comment.type;
			zmCore.getJSON(url,
				{'dataType': 'json'},
				function(data) {
					if(!data.is_error)
					{
						zm('#'+MP3Comment.container).html(data.result);
						zm('#'+MP3Comment.container).attr('cpage',page);
						zm('#_pagination').html(data.paging);
						zm('#_commentCounter').html('('+data.total+')');
						MP3Comment.renderPage();
						//zm('.txtComment').renderEmotions();
						if(typeof ZME_widget_mix =='function') ZME_widget_mix();
						else zmCore.addScript(MP3.ME_WIDGET_URL + 'js/zingme_widget_ex_1.7.1.min.js', function () {ZME_widget_mix();});
					}
			});
		}
	}
};
MP3Comment.init();
